package com.orange.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {

	/**
	 * 获取距离当前系統时间指定时间差的时间（往前）
	 * @param difTime 相隔时间
	 * @param timeType 时间类型，年:year, 月:month, 天:day, 小时:hour, 分钟:minute, 秒:secend
	 * @param formatType 时间类型，如yyyy-MM-dd HH:mm:ss | yyyyMMddHHmmss
	 * @return
	 */
	public static String getDifTimeBefore(int difTime,String timeType,String formatType){
		SimpleDateFormat format = new SimpleDateFormat(formatType);
		Calendar cal = Calendar.getInstance();
		if(timeType.equals("year")){
			cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)-difTime);
		}else if(timeType.equals("month")){
			cal.add(Calendar.MONTH, -difTime);
//			cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-difTime);
		}else if(timeType.equals("day")){
			cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-difTime);
		}else if(timeType.equals("hour")){
			cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY)-difTime);
		}else if(timeType.equals("minute")){
			cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE)-difTime);
		}else if(timeType.equals("secend")){
			cal.set(Calendar.SECOND, cal.get(Calendar.SECOND)-difTime);
		}
		return format.format(cal.getTime());
	}
	
	/**
	 * 输入date是否大于当前时间往前推相隔时间以前，大于返回true
	 * @param date 输入date
	 * @param difHour 相隔时间
	 * @return
	 */
	public static boolean ifGreaterSpecialHourWithNow(Date date, int difHour){
		Date dateNow = new Date();	
		if((dateNow.getTime()-date.getTime())/(1000*60*60)>difHour){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 输入的modification time of file in milliseconds是否大于当前时间往前推相隔时间以前，大于返回true
	 * @param fileTime
	 * @param difHour
	 * @return
	 */
	public static boolean ifGreaterSpeHourWithNow(Long fileTime, int difHour){
		Date dateNow = new Date();	
		if((dateNow.getTime()-fileTime)/(1000*60*60)>difHour){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 时间戳到指定时间格式的转换.
	 * @param timestamp 时间戳，类似:1479550872000
	 * @param formatType 时间类型，如yyyy-MM-dd HH:mm:ss | yyyyMMddHHmmss
	 * @return 指定格式时间
	 */
	public static String GetTimeByStamp(String timestamp, String formatType) {
		long datatime = Long.parseLong(timestamp);
		Date date = new Date(datatime);
		SimpleDateFormat format = new SimpleDateFormat(formatType);
		String timeresult = format.format(date);
		return timeresult;
	}
	
	/**
	 * 时间到时间戳的转换。注意时间是字符串格式。字符串与时间的相互转换，此不赘述。
	 * @param time 和时间类型保持一致，比如yyyyMMddHHmmss对应20161119184230
	 * @param formatType 时间类型，如yyyy-MM-dd HH:mm:ss | yyyyMMddHHmmss
	 * @return 时间戳
	 */
	public static long GetStampByTime(String time, String formatType) {
		String Stamp = "";
		SimpleDateFormat sdf = new SimpleDateFormat(formatType);
		Date date;
		try {
			date = sdf.parse(time);
			Stamp = date.getTime() + "";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Long.valueOf(Stamp);
	}

	/**
	 * 获取指定格式时间
	 * @param type yyyyMMddHHmmss
	 * @return
	 */
	public static String getCurrentFormatTime(String type) {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat(type);
		String timeresult = format.format(date);
		return timeresult;
	}

	/**
	 * 时间转换为long型数据（秒）
	 * @param time
	 * @param formatType 时间类型，如yyyy-MM-dd HH:mm:ss | yyyyMMddHHmmss
	 * @return
	 */
	public static long changeTimeToLong(String time, String formatType) {
		long result = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = null;
		Calendar before = null;
		try {
			d = format.parse(time);
			// 给定时间处理
			before = Calendar.getInstance();
			before.setTime(d);
			result = before.getTimeInMillis() / 1000;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取时间差（分钟）
	 * @param beforeTime
	 * @param lastTime
	 * @return
	 */
	public static long getDifTime(long beforeTime, long lastTime) {
		long dayDiff = (lastTime - beforeTime) / (1000 * 60);
		return dayDiff;
	}

	/**
	 * 获取时间差（分钟）
	 * @param beforeTime
	 * @param lastTime
	 * @return
	 */
	public static long getDifTime(String beforeTime, String lastTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = null;
		Calendar before = null;
		Calendar now = null;
		long dayDiff = 0;
		try {
			d = format.parse(beforeTime);
			// 给定时间处理
			before = Calendar.getInstance();
			before.setTime(d);
			// 当前时间处理
			now = Calendar.getInstance();
			now.setTime(format.parse(lastTime));
			dayDiff = (now.getTimeInMillis() - before.getTimeInMillis()) / (1000 * 60);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dayDiff;
	}
	
	/**
	 * UTC时间转换
	 * @param time
	 * @return
	 */
	public static String changeTime(String time) {
		String result = "";
		Long timestamp1 = Long.parseLong(time) * 1000;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		result = format.format(new Date(timestamp1));
		return result;
	}

	/**
	 * 时间格式转换（Date转String）
	 * @param date
	 * @param type
	 * @return
	 */
	public static String timeChangeType(Date date, String type) {
		SimpleDateFormat format = new SimpleDateFormat(type);
		return format.format(date);
	}

	/**
	 * 时间格式转换（String转Date）
	 * @param date
	 * @param type
	 * @return
	 */
	public static Date StringToDate(String date, String type) {
		try {
			SimpleDateFormat format = new SimpleDateFormat(type);
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据时间戳返回指定格式数据
	 * @param l
	 * @return
	 */
	public static String getFormatTimeByStamp(long l){
		Date date = new Date(l);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
		String timeresult = format.format(date);
		return timeresult;
	}
	
	/**
	 * 获取指定格式时间
	 * @return
	 */
	public static String getCurrentFormatTimeSecond() {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String timeresult = format.format(date);
		return timeresult;
	}
	
	/**
	 * @param time
	 * @param type
	 * @param minute
	 * @return
	 */
	public static String getTimeAddMinute(String time,String type,int minute){
		try{
			SimpleDateFormat format = new SimpleDateFormat(type);
			Date d = format.parse(time);
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE)+minute);
			return format.format(cal.getTime());
		}catch(ParseException e){
			e.printStackTrace();
		}
		return "";
	}
	
}
